#include "threadPool.h"

int threadPoolInit(threadPool_t *pThreadPool, int workerNum, int log_fd)
{
    workerInfoInit(&pThreadPool->workerInfo, workerNum);
    taskQueueInit(&pThreadPool->taskQueue);
    pthread_mutex_init(&pThreadPool->mutex, NULL);
    pthread_cond_init(&pThreadPool->cond, NULL);
    pThreadPool->exitFlag = 0;
    pThreadPool->log_fd = log_fd;
    return 0;
}

//配置文件启动函数
void load_config(char *path, struct server_config *config){
    FILE *file = fopen(path, "r");
    if (!file) {
        perror("无法打开配置文件");
        exit(-1);
    }

    char line[256];  //读取一行
    int line_num = 0;
    while (fgets(line, sizeof(line), file)) {
        //移除换行符
        size_t len = strlen(line);
        if (len > 0 && line[len - 1] == '\n') {
            line[len - 1] = '\0';
            len--;
        }
        // 跳过注释行和空行
        if (line[0] == '#' || line[0] == '\n'){
            ++line_num;
            continue;
        }
        int lin_len = strlen(line);
        line[lin_len] = '\0';
        char *key = strtok(line," =");
        if(!key){
            fprintf(stderr, "配置错误(行%d): 缺少键\n", line_num);
            continue;
        }
        if(strcmp(key, "ip") == 0){
            key = strtok(NULL," =\"");
            strncpy(config->ip,key,strlen(key));
        }
        else if(strcmp(key, "port") == 0){
            key = strtok(NULL," =\"");
            config->port = atoi(key);
        }
        else if(strcmp(key, "workerNum") == 0){
            key = strtok(NULL," =\"");
            config->workerNum = atoi(key);
        }
        ++line_num;   
    }
}